package com.itheima.controller;

import com.itheima.domain.Teacher;
import com.itheima.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);

    //开启教师管理系统的方法
    public void start() {
        lo:while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choose = sc.nextLine();
            switch (choose) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    showTeacher();
                    break;
                case "5":
                    System.out.println("退出");
                    break lo;
                default:
                    System.out.println("您输入的选择有误,请重新选择:");

            }
        }
    }

    //添加教师的方法
    private void addTeacher() {
        String sid = null;
        while (true) {
            System.out.println("请输入教师的工号:");
            sid = sc.nextLine();
            if (sid.equals("")) {
                System.out.println("输入的工号不能为空!");
            } else {
                boolean result = teacherService.isExists(sid);
                //result为true时证明已经有相同的存在了,所以为false时sid才可以继续往数组里面存
                if (result) {
                    System.out.println("您输入的教师工号已存在或有误,请重新录入!");
                } else {
                    break;
                }
            }
        }
        Teacher teacher = getTeacher(sid);
        boolean flag = teacherService.addTeacher(teacher);
        if (flag) {
            System.out.println("添加成功");
        }
    }

    //删除教师的方法
    private void deleteTeacher() {
        while (true) {
            System.out.println("请输入要删除的教师工号:");
            String sid = sc.nextLine();
            if (sid.equals("")) {
                System.out.println("请输入工号:");
            } else {
                boolean result = teacherService.isExists(sid);
                if (result) {
                    teacherService.deleteTeacher(sid);
                    System.out.println("删除成功!");
                } else {
                    System.out.println("您要删除的教师工号不存在,请查证后再删!");
                }
                break;
            }
        }
    }

    //修改教师信息的方法
    private void updateTeacher() {
        while (true) {
            System.out.println("请输入要修改的教师工号:");
            String sid = sc.nextLine();
            if (sid.equals("")) {
                System.out.println("工号不能为空,请输入工号:");
            } else {
                boolean result = teacherService.isExists(sid);
                if (result) {
                   /* String name = null;
                    while (true) {
                        System.out.println("请输入教师的新姓名:");
                        name = sc.nextLine();
                        if (name.equals("")) {
                            System.out.println("姓名不能为空,请重新输入!");
                        } else {
                            break;
                        }
                    }
                    String age = null;
                    while (true) {
                        System.out.println("请输入教师的新年龄:");
                        age = sc.nextLine();
                        if (age.equals("")) {
                            System.out.println("年龄不能为空,请重新输入!");
                        } else {
                            break;
                        }
                    }
                    String birthday = null;
                    while (true) {
                        System.out.println("请输入教师的新生日:");
                        birthday = sc.nextLine();
                        if (birthday.equals("")) {
                            System.out.println("生日不能为空,请重新输入!");
                        } else {
                            break;
                        }
                    }
                    Student student = new Student(sid, name, age, birthday);*/
                    Teacher teacher = getTeacher(sid);
                    teacherService.updateTeacher(sid, teacher);
                    System.out.println("修改成功!");
                } else {
                    System.out.println("您要修改的教师工号不存在,请查证后再改!");
                }
                break;
            }

        }
    }

    //查看教师信息的方法
    private void showTeacher() {
        Teacher[] stu = teacherService.showTeacher();
        if (stu != null) {
            System.out.println("工号\t\t姓名\t\t年龄\t\t生日");
            for (int i = 0; i < stu.length; i++) {
                if (stu[i] != null) {
                    System.out.println(stu[i].getSid() + "\t\t" + stu[i].getName() + "\t\t" + stu[i].getAge() + "\t\t" + stu[i].getBirthday());
                }
            }
        } else {
            System.out.println("容器为空,请添加后在查!");
        }
    }

    //键盘录入教师姓名,年龄,生日的方法
    private Teacher getTeacher(String sid) {
        String name = null;
        while (true) {
            System.out.println("请输入教师的姓名:");
            name = sc.nextLine();
            if (name.equals("")) {
                System.out.println("姓名不能为空,请重新输入!");
            } else {
                break;
            }
        }
        String age = null;
        while (true) {
            System.out.println("请输入教师的年龄:");
            age = sc.nextLine();
            if (age.equals("")) {
                System.out.println("年龄不能为空,请重新输入!");
            } else {
                break;
            }
        }
        String birthday = null;
        while (true) {
            System.out.println("请输入教师的生日:");
            birthday = sc.nextLine();
            if (birthday.equals("")) {
                System.out.println("生日不能为空,请重新输入!");
            } else {
                break;
            }
        }
        return new Teacher(sid, name, age, birthday);
    }
}
